
from pyrind import ROptionMenu

from Tkinter import *



class TestCase():
	def __init__(self, tktoplevel):
		self._root = tktoplevel
		self.img1data = 'R0lGODlhGAAYAIABAAAAAP///yH5BAEAAAEALAAAAAAYABgAAAI0jI+py+0PAXxATlbtTVlv03lbKE5kSZ3fWY0surxtRH5BduMuqF9eZzsAg7xZcEg0EpebAgA7'
		self.img2data = 'R0lGODlhGAAYAJECAP///wAAAP///wAAACH5BAEAAAIALAAAAAAYABgAAAJOlI+pixEMGwAvwjCrXZhu1mmfRDkjGZpjmGXi1VLtezgPK+e0kE6+H+IAf8QMKFYEwjpJ47H529WgShhVSmpip9Att+j9EsMGm9l2OhUAADs='
		self.img1 = PhotoImage(format='gif', data=self.img1data)
		self.img2 = PhotoImage(format='gif', data=self.img2data)

		
		menubar = Menu(self._root)
		menuSettings = Menu(menubar, tearoff=0)
		menubar.add_cascade(label="Properties", menu=menuSettings, underline=0)
		#~ menuSettings.add_command(label="a")
		self.menuSettings = menuSettings
		
		self._root.config(menu=menubar)
		self._prevPullName = None
		self._prevPullMenu = None
		self._prevPullInc = 0
		
	def add(self, control, prop, value, **moreopts):
		if prop!=self._prevPullName:
			self._prevPullMenu = Menu(self.menuSettings, tearoff=0)
			self.menuSettings.add_cascade(label=prop, menu=self._prevPullMenu)
			self._prevPullName = prop
			self._prevPullInc = 1
		else:
			self._prevPullInc += 1
		
		def callback():
			control.set_many(**{prop:value})
			if moreopts: control.set_many(**moreopts)
		
		self._prevPullMenu.add_command(label=str(self._prevPullInc), command=callback)
		

class TestCaseOpt():
	def __init__(self, w):
		self.img1data = 'R0lGODlhGAAYAIABAAAAAP///yH5BAEAAAEALAAAAAAYABgAAAI0jI+py+0PAXxATlbtTVlv03lbKE5kSZ3fWY0surxtRH5BduMuqF9eZzsAg7xZcEg0EpebAgA7'
		self.img2data = 'R0lGODlhGAAYAJECAP///wAAAP///wAAACH5BAEAAAIALAAAAAAYABgAAAJOlI+pixEMGwAvwjCrXZhu1mmfRDkjGZpjmGXi1VLtezgPK+e0kE6+H+IAf8QMKFYEwjpJ47H529WgShhVSmpip9Att+j9EsMGm9l2OhUAADs='
		self.img1 = PhotoImage(format='gif', data=self.img1data)
		self.img2 = PhotoImage(format='gif', data=self.img2data)
		
		self.w=w
		
	def addProperty(self, control, prop, listpropvalues, fnAlsoDo=None):
		
		
		def callback(value):
			if fnAlsoDo: fnAlsoDo()
			
			if isinstance(control, tuple):
				for contr in control:
					contr.set_many(**{prop:value})
					print('*%s* set to %s. (reported to be %s)'%(prop, value, contr.get_prop(prop)))
			else:
				control.set_many(**{prop:value})
				print('*%s* set to %s. (reported to be %s)'%(prop, value, control.get_prop(prop)))
		
		
		def fmt(ind, s):
			
			s=str(ind)+str(s)
			s=s.replace('\n','')
			if len(s)>5: s=s[0:5]
			ind+=1
			return prop+':'+s
		
		listpropdisplays=[]
		for i in range(len(listpropvalues)):
			listpropdisplays.append(  fmt(i, listpropvalues[i]))
		
		j = self.w.add_optionmenu(listpropdisplays, listValues=listpropvalues, fnCallback=callback)
		j.font = ('Lucida Console', 7, 'normal')
		
		
	def addRow(self):
		self.w.end_frame()
		self.w.begin_frame()
		
	def addMethod(self, name, fn):
		def callback():
			result = fn()
			print ('running .%s(), result: %s'%(name,result))
		def fmt(s):
			if len(s)>5: s=s[0:5]
			return '.'+s+'()'
			
		btn = self.w.add_button(fmt(name), callback)
		btn.font = ('Lucida Console', 7, 'normal')
		
	
	